/*
 * Utra.h
 *
 *  Created on: 2012-12-24
 *      Author: lijian
 */

#ifndef UART_H_
#define UART_H_
#include <iostream>
#include <vector>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
using namespace std;

typedef enum {
	None, Odd, Even
} parrity_e;



typedef struct {
	string port;
	unsigned int buadRate;
	unsigned char dataBits;
	unsigned char stopBits;
	parrity_e parity;
} UtraConfig;

class Uart {
public:
	Uart();
	virtual ~Uart();
	void setConfig(string port, unsigned int buadRate, unsigned char dataBits,
			unsigned char stopBits, unsigned char parity,
			unsigned char flowControl);
	void setConfig(UtraConfig config);

	void open();
    
    void close();

	int sendString(string str);

	int sendHex(string str);
    
    void sendByte(unsigned char* data,int len);

    bool readData(unsigned char* buff,int *len);
    
	static vector<string> get_all_utras();
    
private:
	int fd;
	long sendSum;
    long recvSum;
    bool opened;
	UtraConfig config;
    unsigned char frame[1024];
    int lastFrameLen;
	void setOpt();

	int write(void* buff,int len);
    
    int read();

};

#endif